<h3><?php echo $this->p_libelle; ?></h3>
<hr>
<div class="container">
    <?php
    if(isset($this->error)) {
        echo '<h3>'.$this->error.'</h3>';
    } else {
        $display_image = 'no-preview.jpg';
        // get the first image which exists
        foreach ($this->p_images as $image) {
            if (file_exists($this->full_file_path.$image->getChemin())) {
                $display_image = $image->getChemin();
                break;
            }
        }
        $display_image = $this->file_path.$display_image;
        ?>
        <div class="col-lg-3 col-md-6 hero-feature">
            <div class="thumbnail">
                <img src="<?php echo $display_image; ?>" alt="">
                <div class="caption">
                    <p>Description : <?php echo $this->p_description; ?></p>
                    <?php
                    if(isset($this->p_taille)) { ?>
                        <p>Taille : <?php echo $this->p_taille; ?></p>
                        <?php
                    } ?>
                    <p>Prix départ : <?php echo $this->p_prix; ?> &euro;</p>
                </div>
            </div>
        </div>
        <div class="col-lg-3 col-md-6 hero-feature">
            <?php
            switch($this->p_etat) {
                case 1: //"En vente"
                    ?>
                    <h3>
                        <?php echo TranslateMonth(date('j M Y  h:i:s e', $this->p_datefin)); ?>
                    </h3>

                    <div class="caption">
                        <a href="<?php echo $this->url('boutiqueFront',
                                        array('id' =>  $this->p_boutique));?>">Envie d'autres articles ?</a>
                    </div>
                    <?php
                    if($this->p_userid != $this->userid) { ?>
                        <div class="caption">
                            <a href="#buy" id="btn_buy" class="btn btn-primary">Acheter maintenant</a>
                        </div>
                        <?php
                    }
                    break;
                case 2: //"En enchère"
                    ?>
                    <h3>
                        Temps restant :
                        <span id="timeleft">Chargement...</span>
                        <span style="font-size:10px;">(<?php echo TranslateMonth(date('j M Y  h:i:s e', $this->p_datefin)); ?>)</span>
                    </h3>

                    <div class="caption">
                        <div id="bidinfos" <?php echo (isset($this->e_montant) ? '' : 'style="display: none;"'); ?>>
                            <p>Mise actuelle : <span id="bidval"><?php echo $this->e_montant; ?></span> &euro;</p>
                            <p>Par : <span id="bid_by"><?php echo $this->e_nom; ?></span></p>
                        </div>
                        
                        <?php
                        if(!isset($this->e_montant)) {
                            //commme il n'y a aucune enchère, le montant de l'enchère est le prix du produit
                            $this->e_montant=$this->p_prix;
                        }
                        
                        if($this->p_userid != $this->userid) {
                            //on teste si l'enchère est terminée
                            if($this->p_datefin-time() > 0) { ?>
                                <p><input type="text" id="bid" /> <span>&euro;</span></p>
                                <a href="#bid" id="btn_bid" class="btn btn-primary" disabled>Enchérir</a>
                                <p>Entrez un prix supérieur à <span id="bidval2"><?php echo $this->e_montant; ?></span> &euro;</p>
                                <a href="<?php echo $this->url('boutiqueFront', array('id' =>  $this->p_boutique));?>">Envie d'autres articles ?</a>
                                <?php
                            }
                        }
                        ?>
                    </div>
                    <?php
                    break;
                case 3: //"Vendu"
                    ?>
                    <h3>
                        Vendu le <?php echo TranslateMonth(date('j M Y  h:i:s e', $this->p_datevente)); ?>
                    </h3>
                    <?php
            }
            ?>
        </div>
        
        <script>        
        $(function() {
            <?php
            if($this->p_etat == 2) { //"En enchère" ?>
                intervalCd();
                setInterval(intervalCd, 1000);
                
                //var latestBid = <?php echo (($this->p_etat == 2) ? $this->e_montant : 0); ?>;
                
                $('#bid').keyup(function() {
                    var f = $(this).val().match(/^\d{1,}(\.\d{1,2})?$/g);

                    if(f !== null) {
                        $('#btn_bid').removeAttr('disabled');
                    } else {
                        $('#btn_bid').attr('disabled', '');
                    }
                });
                
                $('#btn_bid').click(function() {
                    var bid = $('#bid').val();

                    $(this).attr('disabled', '');
                    var _this=this;

                    $.ajax({
                        method: 'post',
                        data: 'bid=' + bid,
                        success: function(res) {
                            if(res.success) {
                                $('#bid').val('');

                                $('#bidinfos').show();

                                var values = res.values;

                                $('#bidval, #bidval2').html(values.e_montant);
                                $('#bid_by').html(values.e_name);
                                //console.log(res);

                                $(_this).removeAttr('disabled');
                            } else if (res.error === 'not_authenticated') {
                                document.location.href = "<?php echo $this->url('zfcuser/login'); ?>";
                            } else if (res.error === 'already_bade') {
                                alert('Vous êtes le dernier enchérisseur !');
                                $('#bid').val('');
                                $(_this).removeAttr('disabled');
                            }
                        },
                        error: function() {
                        }
                    });

                    return false;
                });
                <?php
            } elseif($this->p_etat == 1) { //"En vente" ?>
                $('#btn_buy').click(function() {
                    $(this).attr('disabled', '');
                    var _this=this;

                    $.ajax({
                        method: 'post',
                        data: 'buy=1',
                        success: function(res) {
                            if(res.success) {
                                $(_this).after('<p id="bought" style="display: none;">\
                                    Merci pour votre achat, le vendeur sera notifié et vous contactera.\
                                </p>');
                                $(_this).hide();
                                $('#bought').fadeIn();
                            } else {
                                if (res.error === 'not_authenticated') {
                                    document.location.href = "<?php echo $this->url('zfcuser/login'); ?>";
                                }
                            }
                        },
                        error: function() {
                        }
                    });

                    return false;
                });
                <?php
            }
            ?>
        });
        
        <?php
        if($this->p_etat == 2) { //"En enchère" ?>
            var intervalCd = function() {
                var dateEnd = new Date(<?php echo $this->p_datefin * 1000; ?>);
                var timeLeft = (dateEnd - new Date().getTime()) / 1000;

                if(timeLeft <= 0) {
                    clearInterval(intervalCd);

                    $('#timeleft').html('Terminé');
                    return;
                }

                var res = '';

                var days = 0;
                if(timeLeft >= 86400) {
                    days = parseInt(timeLeft / 86400);
                    res += Math.round(days) + 'j ';
                }
                timeLeft -= days * 86400;

                var hours = 0;
                if(timeLeft >= 3600) {
                    hours = parseInt(timeLeft / 3600);
                    res += Math.round(hours) + 'h ';
                }
                timeLeft -= hours * 3600;

                var mins = 0;
                if(timeLeft >= 60) {
                    mins = parseInt(timeLeft / 60);
                    res += Math.round(mins, 0) + 'm ';
                }
                timeLeft -= mins * 60;
                if(timeLeft > 0) res += Math.round(timeLeft) + 's';

                $('#timeleft').html(res);
            };
            <?php
        }
        ?>
        </script> 
        <?php
    }
    ?>
</div>